<?php
class Controller_Admin_Customize extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'customize';
        parent::__construct($req, $res);

        $this->_list_where = array(
//            array('status', '=', 'VALID'),
        );
    }

    public function action_demo ()
    {
    }

    public function action_index ($page = 1)
    {
        $this->action_list($page);
    }

    public function action_list($page = 1){
        $page     = max(1, $page);
        $limit    = 10;
        $offset   = $limit * ($page - 1);


        list($counts,$data) = User_Customize::getCustomizeData(-1,$limit,$offset);
//        $counts = "";
//        var_dump($counts, $limit);
        $pagination = $this->pagination($counts, $limit);
        $this->template->set('pagination', $pagination);
        $this->template->set('data', $data);
//        var_dump($data);
        $this->template->set_filename('smarty:/admin/customize/list');
    }

    public function action_export(){
        set_time_limit(0);
        ini_set('memory_limit', '1024M');
        $export  = DB::select()
            ->from('mod_customize')
            ->execute()->as_array();
//        var_dump($export);exit;
        $title  = array(
            '出行日期',
            '用户姓名',
            '用户性别',
            '联系方式',
            '旅行天数(天)',
            '补充说明',
            '申请时间',
        );

        $data = array();
        foreach ($export as $rs)
        {
            $rs['created'] = date("Y.m.d H:i:s",$rs['created']);
            $data[] = array(
                $rs['tripTime'],
                $rs['userName'],
                $rs['gender'],
                $rs['mobile'],
                $rs['tripLength'],
                $rs['intro'],
                $rs['created'],
            );
        }
        tools_excel::export2($data, $title, "定制数据");
    }



    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }

        parent::action_create();
    }

    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
        }

        parent::action_update($nid);
    }

    public function action_delete ($nid)
    {
        $scenic = new ScenicSpot($nid);
        $scenic->remove($nid);
        $this->to_url('list');
    }

    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 

        $this->_form_data = $data;
        parent::action_detail($nid);
    }

    private function rebuild_post_data (& $post)
    {
        //
        $post['managerUid'] = user::current_user_id();

        $post['images'] = implode('|', $post['images']);

        // 起始时间
    }

    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
        //    $rs['type'] = App_Config::getLabelString($rs['type'], 'array');
        }
    }


    public function classname_by_list_row (& $row)
    {
    }

    public function actions_by_list_row (& $row, & $menus)
    {
	    $controller = Request::initial()->controller();
	    $directory  = Request::initial()->directory();
	
	    $directory  = empty($directory) ? '' : '/'.$directory;
    }
}


